<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-product/temp/product/add-new' }">
        <i class="el-icon-s-promotion"></i> 商品管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>新增商品</el-breadcrumb-item>
      <el-breadcrumb-item>第1步：填写商品信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="商品类别" prop="categoryId">
        <el-select v-model="ruleForm.categoryId" placeholder="请选择">
          <el-option
              v-for="item in categoryListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="供应商" prop="mfrs">
        <el-input v-model="ruleForm.mfrs"></el-input>
      </el-form-item>
      <el-form-item label="包装" prop="packing">
        <el-input v-model="ruleForm.packing"></el-input>
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="ruleForm.model"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="规格" prop="standard">
            <el-input v-model="ruleForm.standard"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="ruleForm.color"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="零售价" prop="retailPrice">
            <el-input v-model="ruleForm.retailPrice"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最低售价" prop="lowPrice">
            <el-input v-model="ruleForm.lowPrice"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="库存" prop="inventory">
        <el-input v-model="ruleForm.inventory"></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input v-model="ruleForm.unit"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="ruleForm.remark"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#AAAAAA">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categoryListOptions: [],
      ruleForm: {
        categoryName:'',
        categoryId: '',
        mfrs: '',
        packing:'',
        model: '',
        name: '',
        standard: '',
        color: '',
        lowPrice: '',
        unit: '',
        retailPrice: '',
        inventory: '',
        enable: '',
        enableName: '',
        remark: '',
      },
      rules: {
        categoryId: [
          {type: 'number', required: true, message: '请选择类别', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入商品名称', trigger: 'blur'},
        ],
        mfrs: [
          {required: true, message: '请输入供应商', trigger: 'blur'},
        ],
        lowPrice: [
          {required: true, message: '请输入最低', trigger: 'blur'},
        ],
        unit: [
          {required: true, message: '请输入计件单位', trigger: 'blur'},
        ],
        retailPrice: [
          {required: true, message: '请输入售价', trigger: 'blur'},
        ],
        inventory: [
          {required: true, message: '请输入售价', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('ruleForm');
      if (localRuleFormString) {
        let localRuleForm = JSON.parse(localRuleFormString);
        this.ruleForm=localRuleForm;
      }
    },
    loadCategoryList() {
      let url = 'http://localhost:8080/categories';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.categoryListOptions = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/categories/' + this.ruleForm.categoryId;
          console.log('url = ' + url);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.ruleForm.categoryName = responseBody.data.name;
              console.log("responseBody.data.enable"+responseBody.data.enable)

              let ruleFormString = JSON.stringify(this.ruleForm);
              console.log('ruleFormString >>> ' + ruleFormString);
              localStorage.setItem('ruleForm', ruleFormString);
              this.$router.push('add-new2');
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      localStorage.removeItem('ruleForm');
    }
  },
  mounted() {
    this.loadCategoryList();
    this.loadLocalRuleForm();
  }
}
</script>